<?php

class Application_Model_StanMapper
{
protected $_dbTable;

    public function init()
  	{
  		$this->_dbTable = new Application_Model_DbTable_Stan();
  	}

	public function setDbTable($dbTable)
	{
		$dbTable = new $dbTable();

        	if (!$dbTable instanceof Zend_Db_Table_Abstract)
        	{
            		throw new Exception('Invalid table data gateway provided');
        	}

        	$this->_dbTable = $dbTable;
        	return $this;
    	}

    	public function getDbTable()
    	{
    		if (null == $this->_dbTable)
        	{
            		$this->setDbTable('Application_Model_DbTable_Stan');
        	}
        	return $this->_dbTable;
    	}

        public function dodaj(Application_Model_Stan $ug)
	{
        	$data = array(
				'id_stan' => $ug->getIdStan(),
                                'id_przesylki' => $ug->getIdPrzesylki(),
        		);

        	$this->getDbTable()->insert($data);
    	}

        public function edytuj($id, $id2, Application_Model_Stan $ug)
	{
        	$data = array(
				'id_stan' => $ug->getIdStan(),
                                'id_przesylki' => $ug->getIdPrzesylki(),
        		);

                $where = $this->getDbTable()->getAdapter()->quoteInto('id_stan = ? AND id_przesylki = ?', $id, $id2);

        	$this->getDbTable()->update($data,$where);
    	}

    	public function usun2($id,$id2)
	{
		$where = $this->getDbTable()->getAdapter()->quoteInto('id_user = ? AND id_rola = ?', $id, $id2);
        	$this->getDbTable()->delete($where);
    	}

        public function usun($id)
	{
		$where = $this->getDbTable()->getAdapter()->quoteInto('id_user = ?', $id);
        	$this->getDbTable()->delete($where);
    	}

        public function zwroc($id,$id2, Application_Model_Stan $ug)
    	{
                $db = Zend_Db_Table::getDefaultAdapter();
    		$select =$db->select()
    				->from('stan')
                                ->where("id_user = ? AND id_rola = ?", $id, $id2);

                $wyrazenie = $db->query($select);
    		$resultSet = $wyrazenie->fetchAll();

                foreach($resultSet as $row)
                {
                    $ug->setIdUser($row['id_user']);
                    $ug->setIdRola($row['id_rola']);
                }
   	}

        public function zwrocDlaUser($id)
    	{
                $where = $this->getDbTable()->getAdapter()->quoteInto('id_user = ?', $id);
                $resultSet = $this->getDbTable()->fetchAll($where);

                $ugA = array();

                foreach($resultSet as $row)
                {
                    $rola = $row->findParentRow('Application_Model_DbTable_Groups','Groups');
                    $ug = new Application_Model_UserGroups();

                    $ug->setIdUser($row['id_user']);
                    $ug->setIdRola($row['id_rola']);
                    $ug->setRola($rola['rola']);

                    $ugA[] = $ug;
                }

                return $ugA;
   	}

        public function rolaCount($id)
        {
            $db = Zend_Db_Table::getDefaultAdapter();
            $select =$db->select()
    		->from(array('ug' => 'user_groups'), array('ug.id_user','COUNT(ug.id_rola) as ilosc'))
                ->group('ug.id_user')
                ->where('id_user = ?', $id);

            $wyrazenie = $db->query($select);
            $resultSet = $wyrazenie->fetchAll();

            foreach($resultSet as $row)
            {
                $ilosc = $row['ilosc'];
            }

            return $ilosc;
        }

}

